package com.ysxsoft.wdg.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ysxsoft.wdg.R;

public class ActivityDialog extends Dialog {
    private Context mContext;
    private OnDialogClickListener listener;
    TextView content;
    String keyword = "";

    public OnDialogClickListener getListener() {
        return listener;
    }

    public void setListener(OnDialogClickListener listener) {
        this.listener = listener;
    }

    public ActivityDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
        init();
    }

    public void setText(String keyword) {
        this.keyword = keyword;
    }

    private View init() {
        View view = View.inflate(mContext, R.layout.dialog_activity, null);
        ImageView close = view.findViewById(R.id.close);
        ImageView icon = view.findViewById(R.id.icon);//图片
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        content = view.findViewById(R.id.content);//内容
        content.setText(keyword == null ? "" : keyword);
        Button call = view.findViewById(R.id.btn);//查看详情
        call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.lookDetail();
                    dismiss();
                }
            }
        });
        return view;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(true);
        setContentView(init());
    }

    public void showDialog() {
        if (!isShowing()) {
            show();
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.width = getDisplayWidth(mContext) * 2 / 3;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            getWindow().setAttributes(lp);
            getWindow().setGravity(Gravity.CENTER);
        }
    }

    public int getDisplayWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }


    public interface OnDialogClickListener {
        void lookDetail();
    }
}

